// pages/cooperation/index.js
var app = getApp();
const utils = require('../../utils/util.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    info: {},
    createStaffBalance: 0,//名额
    usedStaffBalance: 0,//已使用名额
    isred: false,
    tagindex: 1,//1共享卖券2联盟代理
    showDialog: false,
    name: '',
    phone: '',
    provinces: [],
    citys: [],
    districts: [],
    selProvince: '请选择',
    selCity: '请选择',
    selDistrict: '请选择',
    selProvinceIndex: 0,
    selCityIndex: 0,
    selDistrictIndex: 0,
    PCList: [],//所有省市区总裁
    provinceBossList: [],//省总裁
    cityBossList: [],//市总裁
    disBossList: [],//区总裁
    regionId: 0
  },

  changeTag: function(e){
    let tagindex = e.currentTarget.dataset.tagindex;
    if(tagindex == 1){//共享卖券
      this.getRegionVersion();
    }
    if(tagindex == 2){//联盟代理
      this.getVersion();
      this.getMyPayRequests();
      this.getDistributor();
    }
    this.setData({
      tagindex: tagindex
    })
  },
  // 申请区县独家
  applyChairman: function(){
    this.getProvince();
    this.getAiCardInfo();
    this.setData({
      showDialog: !this.data.showDialog
    })
  },
  closedialog: function(){
    this.setData({
      showDialog: !this.data.showDialog
    })
  },
  getAiCardInfo: function () {
    var that = this;
    let cardid = wx.getStorageSync('cardId');
    wx.request({
      url: app.globalData.host + '/api/Staff/GetInfo?id=' + cardid,
      success: function (res) {
        if (res.data.Code == 0) {
          let cardInfo = res.data.Data;
          that.setData({
            selProvince: cardInfo.ProvinceName,
            selCity: cardInfo.CityName,
            selDistrict: cardInfo.AreaName,
            regionId: cardInfo.Regionid,
            name: cardInfo.Name,
            phone: cardInfo.Mobile
          })
        }
      }
    })
  },
  // 获取所有区县总裁
  getCityBoss: function(){
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/distributor/GetDisRegionList?uid=' + app.globalData.userInfo.Uid,
      success: function(res){
        if(res.data.Code == 0){
          if(res.data.Data && res.data.Data.length > 0){
            let PCList = res.data.Data;
            let provinceBossList = [];
            let cityBossList = [];
            let disBossList = [];
            for(let i = 0; i < PCList.length; i++){
              if(PCList[i].Layer == 1){//省
                provinceBossList.push(PCList[i]);
              }else if(PCList[i].Layer == 2){//市
                cityBossList.push(PCList[i]);
              }else{//区
                disBossList.push(PCList[i]);
              }
            }
            that.setData({
              provinceBossList: provinceBossList,
              cityBossList: cityBossList,
              disBossList: disBossList,
              PCList: PCList
            })
          }
          
        }
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if(app.globalData.userInfo){
      this.setData({
        userInfo: app.globalData.userInfo
      })
    }
    this.getRegionVersion();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    
  },
  //获取拓展员版本
  getRegionVersion: function(){
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/staff/GetRegionVersion?uid=' + app.globalData.userInfo.Uid,
      success: function(res){
        if(res.data.Code == 0){
          if(res.data.Data){
            if(res.data.Data.upgrade.level == 6){
              that.getCityBoss();
            }
            that.setData({
              info: res.data.Data
            })
          }
        }
      },
    })
  },
  //获取我的联盟代理版本
  getVersion: function() {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Staff/GetVersion?uid=' + app.globalData.userInfo.Uid + '&storeId=' + app.globalData.storeId,
      success: function(res) {
        if (res.data.Code == 0) {
          that.setData({
            info: res.data.Data
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false,
          })
        }
      }
    })
  },
  //升级合作
  upgradeWeapp: function() {
    let that = this;
    let info = that.data.info.upgrade;
    wx.showModal({
      title: '提示',
      content: info.gradeName,
      success(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.host + '/api/Staff/UpgradeWeapp?uid=' + app.globalData.userInfo.Uid + '&level=' + info.level,
            success: function(res) {
              if (res.data.Code == 0) {
                 wx.showToast({
                   title: '提交成功!'
                 })
              }else{
                wx.showModal({
                  title: '提示',
                  content: res.data.Message,
                  showCancel: false
                })
              }
            }
          })
        }
      }
    })
  },
  //合作续费
  renewalWeapp:function(){
    wx.showModal({
      title: '提示',
      content: "申请续费,继续享受尊贵权益",
      success(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.host + '/api/Staff/RenewalWeapp?uid=' + app.globalData.userInfo.Uid,
            success: function (res) {
              if (res.data.Code == 0) {
                wx.showToast({
                  title: '提交成功!'
                })
              }else{
                wx.showModal({
                  title: '提示',
                  content: res.data.Message,
                  showCancel: false
                })
              }
            }
          })
        }
      }
    })
  },
  //获取免费名额
  getDistributor: function () {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Distributor/GetDistributor?userId=' + app.globalData.userInfo.Uid + '&showChild=0&showGrade=0',
      success: function (res) {
        if (res.data.Code == 0) {
          that.setData({
            createStaffBalance: res.data.Data.CreateStaffBalance,//名额
            usedStaffBalance: res.data.Data.UsedStaffBalance//已使用名额
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false,
          })
        }
      }
    })
  },
  //申请免费额度扩容
  requestStaffBalance: function () {
    wx.request({
      url: app.globalData.host + '/api/Distributor/RequestStaffBalance?uid=' + app.globalData.userInfo.Uid,
      success: function (res) {
        if (res.data.Code == 0) {
          wx.showToast({
            title: '申请成功'
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },
  //获取待确认列表
  getMyPayRequests: function () {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Staff/GetMyPayRequests?uid=' + app.globalData.userInfo.Uid + '&status=0' + '&page=1',
      success: function (res) {
        if (res.data.Code == 0) {
          let list = res.data.Data;
          if (list.length > 0) {
            that.setData({
              isred: true
            })
          } else {
            that.setData({
              isred: false
            })
          }
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false,
          })
        }
      }
    })
  },
  //申请区域总裁
  subApply: function(){
    let that = this;
    let name = that.data.name;
    let phone = that.data.phone;
    let selProvinceIndex = that.data.selProvinceIndex;
    let selCityIndex = that.data.selCityIndex;
    let selDistrictIndex = that.data.selDistrictIndex;
    let regionId = that.data.regionId;
    if(selDistrictIndex > 0){
      regionId = selDistrictIndex;
    }else if(selCityIndex > 0){
      regionId = selCityIndex;
    }else if(selProvinceIndex > 0){
      regionId = selProvinceIndex;
    }
    if (name == "") {
      wx.showModal({
        title: '提示',
        content: '请填写联系人姓名',
        showCancel: false
      })
      return;
    }
    if(phone == "" || !utils.vailPhone(phone)){
      wx.showModal({
        title: '提示',
        content: '请检查手机号',
        showCancel: false
      })
      return;
    }
    if (regionId <= 0) {
      wx.showModal({
        title: '提示',
        content: '请选择地区',
        showCancel: false
      })
      return;
    }
    wx.showLoading({
      title: '正在发送申请...',
      mask: true
    })
    wx.request({
      url: app.globalData.host + '/api/staff/ApplyDisRegion?uid='+ app.globalData.userInfo.Uid +'&regionId=' + regionId + '&name=' + name + '&mobile=' + phone + '&level=' + that.data.info.upgrade.level,
      success: function(res){
        if(res.data.Code == 0){
          wx.hideLoading();
          wx.showToast({
            title: '发送成功'
          })
          that.setData({
            showDialog:　!that.data.showDialog
          })
        }else{
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },
  bindName: function(e){
    this.setData({
      name: e.detail.value
    })
  },
  bindPhone: function(e){
    this.setData({
      phone: e.detail.value
    })
  },
  /**
   * 获取省接口
   */
  getProvince: function (e) {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/Tool/ProvinceList',
      success: function (res) {
        var provincesArray = JSON.parse(res.data.Data)
        var pinkArray = [];
        for (var i = 0; i < provincesArray.length; i++) {
          pinkArray.push(provincesArray[i].name);
        }
        that.setData({
          provinces: pinkArray
        })
      }
    })
  },
  //省值选择变化
  bindPickerProvinceChange: function (event) {
    var selIterm = this.data.provinces[event.detail.value]
    var provinceId = parseInt(event.detail.value) + 1
    this.setData({
      selProvince: selIterm,
      selProvinceIndex: provinceId,
      selCity: '请选择',
      selDistrict: '请选择'
    })
    this.getCityList(provinceId)//初始化市数据
  },
  /**
 * 获取市接口
 */
  getCityList: function (provinceid) {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/Tool/CityList?provinceId=' + provinceid,
      success: function (res) {
        var cityList = JSON.parse(res.data.Data)
        var pinkCitys = [];
        for (var i = 0; i < cityList.length; i++) {
          pinkCitys.push(cityList[i].name);
        }
        that.setData({
          citys: pinkCitys,
          cityList: cityList
        })
      }
    })
  },
  //市值选择
  bindPickerCityChange: function (event) {
    var cityList = this.data.cityList;
    var selIterm = this.data.citys[event.detail.value];
    for (var i = 0; i < cityList.length; i++) {
      if (selIterm == cityList[i].name) {
        var citysId = cityList[i].id;
      }
    }
    this.setData({
      selCity: selIterm,
      selCityIndex: citysId,
      selDistrict: '请选择'
    })
    this.getCountyList(citysId)
  },
  /**
* 当点击获取县地区
*/
  getCountyList: function (citysid) {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/Tool/CityList?provinceId=' + citysid,
      success: function (res) {
        var CountyList = JSON.parse(res.data.Data)
        var pinkCounty = [];
        for (var i = 0; i < CountyList.length; i++) {
          pinkCounty.push(CountyList[i].name);
        }
        that.setData({
          districts: pinkCounty,
          CountyList: CountyList
        })
      }
    })
  },
  bindPickerChange: function (event) {
    var CountyList = this.data.CountyList
    var selIterm = this.data.districts[event.detail.value]
    for (var i = 0; i < CountyList.length; i++) {
      if (selIterm == CountyList[i].name) {
        var DistrictId = CountyList[i].id
      }
    }
    this.setData({
      selDistrict: selIterm,
      selDistrictIndex: DistrictId
    })
  },
  toStaffList: function(e){
    // let regionId = e.currentTarget.dataset.regionid
    let layer = e.currentTarget.dataset.layer;
    let regionIds='';
    let list = [0];
    if(layer == '1'){
      list = this.data.provinceBossList;
    }
    if(layer == '2'){
      list = this.data.cityBossList;
    }
    if(layer == '3'){
      list = this.data.disBossList;
    }
    for(var i = 0; i < list.length; i++){
      regionIds += regionIds == ''? '' : ',';
      regionIds += list[i].RegionId;
    }
    console.log(regionIds)
    wx.navigateTo({
      url: '/fenxiao/cooperationList/index?regionId=' + regionIds + '&layer=' + layer,
    })
  }
})